/**
 *  Copyright (C) 2006 zhangbo (freeeob@gmail.com)
 *
 *  This product is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1 of the License, or
 *  (at your option) any later version.
 * 
 *  This product is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 *
 *  author:zhangbo
 *  Email:jsjava@gmail.com
 */
 
function CursorUtils(){
	this.jsjava_class="jsorg.eob.document.CursorUtils";
}

/**
 * Get cursor position in text area
 * @param textObj a input text or textarea object
 */
CursorUtils.getCursorPosition=function(textObj){
	var ps=CursorUtils.getSelectPositions(textObj);
	return ps[0];
};

/**
 * Get cursor start position in text area
 * @param textObj a input text or textarea object
 */
CursorUtils.getSelectStartPosition=function(textObj){
	var ps=CursorUtils.getSelectPositions(textObj);
	return ps[0];
};

/**
 * Get cursor end position in text area
 * @param textObj a input text or textarea object
 */
CursorUtils.getSelectEndPosition=function(textObj){
	var ps=CursorUtils.getSelectPositions(textObj);
	return ps[1];
};

/**
 * Get cursor select start and end position in text area
 * @param textObj a input text or textarea object
 */
CursorUtils.getSelectPositions=function(textObj){
	if(!DocumentUtils.isHTMLTextObject(textObj)){
		throw new IllegalArgumentException(IllegalArgumentException.ERROR,"The argument must be a text input object!");
	}
	var start = 0, end = 0;
    if(BrowserUtils.isFirefox()){
        start = textObj.selectionStart;
        end = textObj.selectionEnd;
    }else if(BrowserUtils.isIE()) {
        var textRange = document.selection.createRange();
        if(textRange.parentElement() == textObj) {
            var docRange;
            if(DocumentUtils.isHTMLTextArea(textObj)){
            	docRange = document.body.createTextRange();
            	docRange.moveToElementText(textObj);
            }else{
            	docRange = textObj.createTextRange();
            }
            for (start=0; docRange.compareEndPoints("StartToStart", textRange) < 0; start++){
                docRange.moveStart('character', 1);
            }
            for (var i = 0; i <= start; i ++) {
                if (textObj.value.charAt(i) == '\n'){
                	start++;
                }
            }         
            if(DocumentUtils.isHTMLTextArea(textObj)){
            	docRange = document.body.createTextRange();
            	docRange.moveToElementText(textObj);
            }else{
            	docRange = textObj.createTextRange();
            }
            for (end = 0; docRange.compareEndPoints('StartToEnd', textRange) < 0; end ++) {
                docRange.moveStart('character', 1);
            }
            for (var i = 0; i <= end; i ++) {
                if (textObj.value.charAt(i) == '\n')
                    end ++;
            }
        }        
    }
    return [start,end];
};

/**
 * Insert text in current cursor position
 * @param textObj a input text or textarea object
 */
CursorUtils.insertTextInCusorPosition=function(textObj,text){
	if(!(text instanceof String)&&typeof(text)!="string"){
		return;
	}
	var pos=CursorUtils.getCursorPosition(textObj);	
	textObj.value=textObj.value.slice(0,pos)+text+textObj.value.slice(pos);	
	CursorUtils.moveCursorTo(textObj,pos+text.length);
};

/**
 * Insert text in specified position
 * @param textObj a input text or textarea object
 */
CursorUtils.insertTextInPosition=function(textObj,text,pos){
	if(!(text instanceof String)&&typeof(text)!="string"){
		return;
	}
	textObj.value=textObj.value.slice(0,pos)+text+textObj.value.slice(pos);	
	CursorUtils.moveCursorTo(textObj,pos+text.length);
};

/**
 * Move cursor to specified postion
 * @param textObj a input text or textarea object
 * @param pos
 */
CursorUtils.moveCursorTo=function(textObj,pos){
	if(!DocumentUtils.isHTMLTextObject(textObj)){
		throw new IllegalArgumentException(IllegalArgumentException.ERROR,"The argument must be a text input object!");
	}
	if(isNaN(pos)){
		throw new IllegalArgumentException(IllegalArgumentException.ERROR,"position must be a number!");
	}
	if(BrowserUtils.isIE()){
		var textRange=textObj.createTextRange();
		textRange.collapse(true);
		textRange.moveStart("character",pos);		
		textRange.select();
	}else if(BrowserUtils.isFirefox()){
		textObj.setSelectionRange(pos,pos);
		textObj.focus();
	}
};

/**
 * Select text from start to end
 * @param textObj a input text or textarea object
 * @param start
 * @param end
 */
CursorUtils.textSelect=function(textObj,start,end){
	if(!DocumentUtils.isHTMLTextObject(textObj)){
		throw new IllegalArgumentException(IllegalArgumentException.ERROR,"The argument must be a text input object!");
	}
	if(isNaN(start)||isNaN(end)||start>end){
		throw new IllegalArgumentException(IllegalArgumentException.ERROR,"start and end must be a number!");
	}
	if(BrowserUtils.isIE()){
		var textRange=textObj.createTextRange();
		textRange.collapse(true);
		textRange.moveStart("character",start);	
		textRange.moveEnd("character",end-start);		
		textRange.select();
	}else if(BrowserUtils.isFirefox()){
		textObj.setSelectionRange(start,end);
		textObj.focus();
	}
};